package com.kaikeba.estack.common.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Auth xingwan
 * @Date 2020/9/6 23:18
 **/
public class MapRowMapper implements RowMapper<Map<String, Object>> {

    private MapRowMapper() {
    }

    private static final MapRowMapper MAP_ROW_MAPPER = new MapRowMapper();

    @Override
    public Map<String, Object> mapRow(ResultSet resultSet, long rowNum) throws SQLException {
        ResultSetMetaData rsMeta = resultSet.getMetaData();
        int size = rsMeta.getColumnCount();
        Map<String, Object> row = new UnderScoreLinkedHashMap<>(size);
        for (int i = 0; i < size; ++i) {
            String columnName = rsMeta.getColumnLabel(i + 1);
            Object value = resultSet.getObject(i + 1);
            row.put(columnName, value);
        }
        return row;
    }

    public static MapRowMapper getInstance() {
        return MAP_ROW_MAPPER;
    }
}
